/*
 * 创建日期 2005-7-29
 *
 */
package wangjg.commons.character;

import java.util.UUID;

/**
 * 生成随机字符串的类
 * @author wangjg
 * 
 */
public class Random {

	public static String getRandomString(int length) {
		return getRandomString(null, length);
	}

	public static String getRandomString(String prefix, int length) {
		StringBuffer result = new StringBuffer(length);
		if (prefix != null)
			result.append(prefix);
		String bound = "_";
		java.util.Random rnd = new java.util.Random();

		while (result.length() < length) {
			int n = rnd.nextInt(255);
			char c = (char) n;
			if ((n >= 48 && n <= 57) || (n >= 65 && n <= 90)
					|| (n >= 97 && n <= 122) || (isIn(c, bound))) {
				result.append(c);
			}
		}
		return result.toString();
	}

	public static String getUniqueRandomString(String prefix) {
		prefix = prefix != null ? prefix : "";
		String str = UUID.randomUUID().toString();
		//str = Char.BASE64Encode(str);
		return prefix + str;
	}

	public static String getRandomStringbByTime() {
		return getRandomStringbByTime(null, "yyyyMMddHHmmssSSS");
	}
	public static String getRandomStringbByTime(String prefix) {
		return getRandomStringbByTime(prefix, "yyyyMMddHHmmssSSS");
	}
	public static String getRandomStringbByTime(String prefix, String format) {
		String result = prefix != null ? prefix : "";
		try {
			java.text.DateFormat df = new java.text.SimpleDateFormat(format);
			java.util.Date date = new java.util.Date();
			result += df.format(date);
		} catch (Exception ex) {

		}
		return result;
	}
	
	public static String getRandomNumbers(int length) {
		java.util.Random rnd = new java.util.Random();
		StringBuffer result = new StringBuffer(length);
		while (result.length() < length) {
			int n = rnd.nextInt(10);
			result.append(n);
		}
		return result.toString();
	}
	
	public static String getRandomNumbers(int minLen, int maxLen) {
		java.util.Random rnd = new java.util.Random();
		int length = minLen + rnd.nextInt(maxLen-minLen+1);
		StringBuffer result = new StringBuffer(length);
		while (result.length() < length) {
			int n = rnd.nextInt(10);
			result.append(n);
		}
		return result.toString();
	}
	
	public static int getRandomInt(int minVal, int maxVal){
		if(minVal==maxVal){
			return maxVal;
		}
		java.util.Random rnd = new java.util.Random();
		int result = minVal + rnd.nextInt(maxVal-minVal+1);
		return result;
	}
	
	public static Integer getRandomInteger(int length) {
		String str = getRandomNumbers(length);
		return Integer.parseInt(str);
	}
	
	public static Long getRandomLong(int length) {
		String str = getRandomNumbers(length);
		return Long.parseLong(str);
	}

	private static boolean isIn(char c, String bound) {
		if (bound == null)
			return false;
		for (int i = 0; i < bound.length(); i++) {
			if (bound.charAt(i) == c) {
				return true;
			}
		}
		return false;
	}

	public static void main(String[] a) {
		for (int i = 0; i < 1; i++)
			System.out.println(Random.getRandomStringbByTime("uploadfile_"));
		System.out.println(Random.getRandomString(9));
		System.out.println(Random.getUniqueRandomString("upload_"));
		System.out.println(Random.getRandomNumbers(10));
		for(int i=0;i<20;i++)
			System.out.println(Random.getRandomNumbers(5,10));
	}
}
